<?php
header("content-type:text/html;charset=utf-8");
session_start();
include "public/is_login.php";
include "../public/config/config.php";
include "../public/include/funcs.php";

$id = $_GET['id'] ? $_GET['id'] : $_SESSION['id'];
if(!$id) $id = $did;

if($id == 0){
	echo "<script>alert('参数错误！');</script>";
//	echo "您已经是顶级会员，没有上级。";
	header('refresh:2;url=user.php');
}else{
	$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
	$arr = select($link, "user", "id=$id");
	$name = $arr[0]['tname'] ? $arr[0]['tname'] : $arr[0]['nickname'];
	$u_img = $arr[0]['u_img'];
	
	$tname = $arr[0]['tname'];
	$tel = $arr[0]['tel'];
	$age_y = $arr[0]['age_y'];
	$age_m = $arr[0]['age_m'];
	$age_d = $arr[0]['age_d'];
	$xl = $arr[0]['xl'];
	$zy = $arr[0]['zy'];
	$aihao = $arr[0]['aihao'];
	
	$pid = $arr[0]['pid'];
	if($pid == 0){
		$pname = "您没有推荐人";
	}else{
		$parr = select($link, "user", "id=$pid");
		$pname = $parr[0]['tname'] ? $parr[0]['tname'] : $parr[0]['nickname'];
	}
	
}


if($_POST){
	if($login){
		$add_arr['tname'] = $_POST['tname'];
		$add_arr['tel'] = $_POST['tel'];
		$add_arr['age_y'] = $_POST['year'];
		$add_arr['age_m'] = $_POST['month'];
		$add_arr['age_d'] = $_POST['day'];
		$add_arr['xl'] = $_POST['xl'];
		$add_arr['zy'] = $_POST['zy'];
		$add_arr['aihao'] = $_POST['aihao'];
		$re = save($link, "user", $add_arr, "id=$id");
		if($re){
			echo "<script>";
			echo "alert('信息完善成功！');";
			echo "window.location.href='user.php';";
			echo "</script>";
		}else{
			echo "<script>";
			echo "alert('信息完善失败！');";
//			echo "window.location.href='user.php';";
			echo "</script>";
		}
	
	}else{
		echo "<script>";
		echo "alert('非法请求！');";
		echo "window.location.href='login.php';";
		echo "</script>";
	}

}
mysqli_close($link);

//echo "<pre>";
//print_r($arr);
//print_r($parr);
//echo "</pre>";
?>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
	<title>分享商城 -- 完善信息</title>
	
	<!-- Bootstrap -->
	<link href="/public/css/bootstrap.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="public/main.css"/>
	
	<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
	<!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
	<!--[if lt IE 9]>
	<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
	<![endif]-->
</head>
<body class="bg_lit">

<!--<div class="container">
	<img class="list-logo" src="imgs/list/logo.png"/>
</div>-->

<div class="head_top">
	<div class="u_img">
		<img class="img-rounded" src="<?php echo $u_img;?>"/>
	</div>
	<div class="u_name">
		<p class="u_title"><?php echo $name;?></p>
		<p class="u_id">ID号：<?php echo $id;?></p>
	</div>
	<div class="clear"></div>
</div>

<div class="container">
	<p class="wans_info">完善个人信息：</p>
	<div class="info_form">
		<form action="#" method="post" class="form-horizontal" name="frm" onsubmit="return check()">
			<div class="form-group mar_top">
				<label for="tname" class="control-label col-xs-4">姓名：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" name="tname" id="tname" value="<?php echo $tname;?>" />
				</div>
			</div>
			
			<div class="form-group mar_top">
				<label for="tel" class="control-label col-xs-4">电话：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" name="tel" id="tel" value="<?php echo $tel;?>" />
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="year" class="control-label col-xs-4">出生年月日：</label>
				<div class="col-xs-8">
					<input type="number" class="year form-control" name="year" id="year" value="<?php echo $age_y;?>" />年
					<input type="number" class="month form-control" name="month" id="month" value="<?php echo $age_m;?>" />月
					<input type="number" class="day form-control" name="day" id="day" value="<?php echo $age_d;?>" />日
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="xl" class="control-label col-xs-4">学历：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" name="xl" id="xl" value="<?php echo $xl;?>" />
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="zy" class="control-label col-xs-4">职业：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" name="zy" id="zy" value="<?php echo $zy;?>" />
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="aihao" class="control-label col-xs-4">爱好：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" name="aihao" id="aihao" value="<?php echo $aihao;?>" />
				</div>
			</div>
			
			<div class="form-group mar_top">
				<label for="pname" class="control-label col-xs-4">推荐人：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" disabled="disabled" name="pname" id="" value="<?php echo $pname;?>" />
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="pid" class="control-label col-xs-4">推荐人ID：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" disabled="disabled" name="pid" id="" value="<?php echo $pid;?>" />
				</div>
			</div>
			
			<input type="submit" class="btn btn-success btn-block btn-lg mar_top" value="提交"/>
		</form>
	</div>
	
	
</div>


<?php include "template/footer.php";?>


<script type="text/javascript">
function check(){
	var tname=document.frm.tname.value;
	var han = /^[\u4e00-\u9fa5]{2,4}$/;
	if(tname==null || tname=='' || !han.test(tname)){
		alert('姓名不符合规范！');
		document.frm.tname.focus();
		return false;
	}
	
	var tel = document.frm.tel.value;
	var retel = /^1\d{10}$/;
	if (!retel.test(tel)){
//		alert('手机号必须是1开头的11位数字');
		alert('请填写正确的手机号！');
		document.frm.tel.focus();
		return false;
	}
	
	var now = new Date();
	var now_year = now.getFullYear();
	var year = parseInt(document.frm.year.value);
	var age = now_year - year;
	if(isNaN(year) || age < 10 || age > 100){
		alert("出生年份不合规范！");
		document.frm.year.focus();
		return false;
	}
	
	var month=parseInt(document.frm.month.value);
	if(isNaN(month) || month < 1 || month > 12){
		alert("错误的出生月份！");
		document.frm.month.focus();
		return false;
	}
	
	var day=parseInt(document.frm.day.value);
	if(isNaN(day) || day < 1 || day > 31){
		alert("错误的出生日！");
		document.frm.day.focus();
		return false;
	}
	
	return true;
}
</script>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script type="text/javascript" src="/public/js/jquery-3.3.1.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
</body>
</html>